/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */

package ctrLayer;

import java.sql.Date;
import java.util.ArrayList;

import modelLayer.Address;
import modelLayer.Booking;
import modelLayer.Customer;
import dbLayer.DbBooking;
import dbLayer.DbConnection;
import dbLayer.DbMaxId;


/**
 * 
 * @author Blackcidy
 * @version 1.0
 */
public class CtrBooking {
	
	private DbBooking dbBooking;
	private CtrAddress ctrAddress;
	private CtrBusinessPartner ctrBusinessPartner;
	private DbConnection con;
	
	/**
	 * 
	 */
	public CtrBooking(){
		dbBooking = new DbBooking();
		ctrAddress = new CtrAddress();
		ctrBusinessPartner = new CtrBusinessPartner();
		con = DbConnection.getInstance();
	}
	/**
	 * Create a booking and insert it into the database.
	 * @param int bookingNo
	 * @param int customerRefNo
	 * @param int customerId
	 * @param ArrayList<Integer> addressIds
	 * @param int storage
	 * @param Date pickUpDate
	 * @param Date deliveryDate
	 * @param int packageType
	 * @param int quantity
	 * @param double price
	 * @param double weight
	 * @param double length
	 * @param double width
	 * @param doulbe height
	 * @return Booking bObj
	 * @throws Exception
	 */
	public Booking create(int bookingNo, int customerRefNo, int customerId, ArrayList<Address> addressList, int storage, Date pickUpDate, Date deliveryDate, int packageType, int quantity, double price, double weight, double length, double width, double height) throws Exception{
		try {
		con.startTransaction();
		Customer cObj = ctrBusinessPartner.retrieveCustomer(customerId);
		int id = DbMaxId.getMaxID("bookingId", "booking") + 1;
		
		for(Address aObj : addressList) {
			if(aObj.getAddressId() == 0) {
				ctrAddress.insertAddress(aObj);
			}
		}
		
		Booking bObj = new Booking(bookingNo, customerRefNo, cObj, addressList, storage, pickUpDate, deliveryDate, packageType, quantity, price, weight, length, width, height);
		bObj.setBookingId(id);
		dbBooking.insert(bObj);
		int count = 0;
		for(Address aObj : addressList){
			ctrAddress.createAddressBooking(aObj, count, id);
			count++;
		}
		
		bObj = dbBooking.retrieveById(id);
		con.commitTransaction();
		return bObj;
		} catch(Exception e) {
			con.rollbackTransaction();
			throw e;
		}
		
	}
	
	/**
	 * Retrieve booking by its booking id.
	 * @param int bookingId
	 * @return Booking bObj
	 */
	public Booking retrieveBooking(int bookingId){
		Booking bObj = dbBooking.retrieveById(bookingId);
		return bObj;
	}

	/**
	 * Retrieves a list of all bookings not associated to a disposition.
	 * @return
	 */
	public ArrayList<Booking> retrieveNonDisposed() {
		ArrayList<Booking> bList = dbBooking.retrieveNonDisposed();
		return bList;
	}
	
	/**
	 * Retrieves a list of all the bookings in the database. This list can be very long, especially after long usage of the application, so use this method with caution.
	 * @return ArrayList<Booking>
	 */
	public ArrayList<Booking> retrieveAll() {
		ArrayList<Booking> bList = dbBooking.retrieveAll();
		return bList;
	}
	
	/**
	 * Update a booking in the database.
	 * @param Booking booking
	 */
	public void update(Booking booking){
		dbBooking.update(booking);
	}
	
	/**
	 * Delete a booking from the database.
	 * @param Booking booking
	 */
	public void delete(Booking booking){
		dbBooking.delete(booking);
	}
	
}
